package com.appbee.gdata.youtube.client.cell;

import javax.inject.Inject;

import com.appbee.core.client.templates.CommonTemplates;
import com.appbee.gdata.youtube.client.search.Entry;
import com.appbee.gdata.youtube.client.search.MediaGroup;
import com.appbee.gdata.youtube.client.search.MediaThumbnail;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;

public class EntryCell extends AbstractCell<Entry>
{
	private final CommonTemplates html;
	
	@Inject
	public EntryCell(CommonTemplates html)
	{
		this.html = html;
	}

	@Override
	public void render(Context context, Entry value, SafeHtmlBuilder sb)
	{
		sb.append(html.div("", SafeHtmlUtils.fromString(value.getTitle())));
		MediaGroup mediaGroup = value.getMediaGroup();
		if(mediaGroup!=null)
		{
			JsArray<MediaThumbnail> thumbnail = mediaGroup.getMediaThumbnail();
			if(thumbnail!=null && thumbnail.length()>0)
			{
				sb.append(html.img("", thumbnail.get(0).getUrl()));
			}
		}		
	}

}
